angular.module('CheckConfirm')
    .controller('CheckConfirm.HomeCtrl', ['$scope', '$state', 'Http', 'imanDialog', 'CheckConfirm.CheckService', 'LfRfid', 'Device',
        function($scope, $state, Http, imanDialog, checkService, LfRfid, Device) {
            var toast;
            checkService.setContainer();
            $scope.data = {
                choice: '',
                megs: [],
                WHouseName: '请扫描货架获取超市'
            };

            $scope.showToast = function(styleMes, block) {
                if (toast) {
                    toast.hide();
                }
                toast = imanDialog.toast({
                    message: styleMes,
                    block: block,
                    position: 'top',
                    duration: false,
                    backdropClickToClose: false,
                    hardwareBackButtonClose: false
                });
            };

            $scope.hideToast = function() {
                if (toast) {
                    toast.hide();
                    toast = null;
                }
            };

            $scope.$on('$ionicView.afterEnter', function() {
                LfRfid.open();
                $scope.data.containerName = checkService.getContainer().Name;
                $scope.showToast('请先扫描球拍的货架信息', true);
                // searchBarcode('0006492131');
            });

            $scope.$on('$ionicView.beforeLeave', function() {
                LfRfid.close();
                $scope.hideToast();
            });

            $scope.$on('LFRFID.DataReceived', function(e, data) {
                searchBarcode(data);
            });

            $scope.beginScan = function() {
                if (!$scope.data.containerName) {
                    imanDialog.toastBottom("请扫描容器信息");
                    return;
                }

                //checkService.setDepartChoice($scope.data.choice);
                $state.go('CheckConfirm-scan');
            };

            function searchBarcode(barcode) {
                imanDialog.showLoading();
                checkService.queryBarcodeMaster(barcode).then(function(data) {
                    if (data.BarcodeType === 3) {
                        checkService.queryContainer(data).then(function(info) {
                            if (!info || info === null) {
                                imanDialog.toastBottom("查找容器失败");
                                return;
                            }
                            $scope.data.containerName = info.Name;
                            $scope.data.WHouseName = checkService.getWareHouse() ? checkService.getWareHouse().Name : '检查数据库,有无对应仓库';
                            checkService.resetDefault();
                            $scope.data.choice = '';

                        }).finally(function() {
                            imanDialog.hideLoading();
                        });
                    }
                }).finally(function() {
                    imanDialog.hideLoading();
                });
            }
        }
    ]);
